package com.song.exception.controller;


import com.song.pojo.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SessionController {

    @GetMapping("/c1")
    public Result cookie1(HttpServletResponse res) {
        res.addCookie(new Cookie("login_username", "admin"));
        return Result.success();
    }

    @GetMapping("/c2")
    public Result cookie2(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            log.info("cookie: {}", cookie.getName());
            log.info("cookie: {}", cookie.getValue());
        }
        return Result.success();
    }

    @GetMapping("/s1")
    public Result session1(HttpSession session) {
        log.info("session--s1: {}", session.hashCode());
        session.setAttribute("login_username", "admin");
        return Result.success();
    }

    @GetMapping("/s2")
    public Result session2(HttpSession session) {
        log.info("session--s2: {}", session.hashCode());
        Object login_username = session.getAttribute("login_username");

        log.info("session--s2: {}", login_username);
        return Result.success(login_username);
    }
}
